package com.mad.base.system;

@SuppressWarnings( "rawtypes" )
public class ID  implements IComparable2D{

	private int x; 
	private char y ;
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public char getY() {
		return y;
	}
	public void setY(char y) {
		this.y = y;
	}
	public ID(int x, char y) {
		this.x = x;
		this.y = y;
	} 
		
	@Override
	public String toString(){
		return y+""+x ; 
	}

    @Override
    public Comparable getComparableFirstDimension()
    {
        return this.getX();
    }
    
    @Override
    public Comparable getComparableSecondDimension()
    {
        return this.getY(); 
    }
    
    @Override
    public boolean greaterThan( Object o )
    {
        return this.compareTo( o ) > 0 ; 
    }
    @Override
    public boolean smallerThan( Object o )
    {
        return this.compareTo( o ) < 0 ; 
    }
    @Override
    public boolean equalTo( Object o )
    {
        return this.compareTo( o ) == 0 ; 
    }

    @Override
    public boolean smallerThanOrEqual(Object o)
    {
        return this.smallerThan( o ) || this.equalTo( o ) ; 
    }
    @Override
    public boolean greaterThanOrEqual(Object o)
    {
        return this.greaterThan( o ) || this.equalTo( o ) ; 
    }
    @Override
    public int compareTo( Object o )
    {
        int res = 0 ; 
        if(o instanceof ID){
            ID tmp = (ID)o ; 
            if(tmp.getY() < this.getY() || (tmp.getY()==this.getY() && tmp.getX() < this.getX())) res = 1 ; 
            else if(tmp.getY() > this.getY() || (tmp.getY()==this.getY() && tmp.getX() > this.getX())) res = -1 ; 
            else if(tmp.getX() == this.getX() && tmp.getY() == this.getY()) res = 0 ; 
        }
        return res ; 
    }
	
	public static ID getIDByCoords(int x, int y){
		return new ID(y+1, (char)(x+65)); 
	}
	
	public static int convertY(char y){
	    return y - 65 ; 
	}
	
	public static int convertX(int x){
	    return x -1 ; 
	}
	
	public static ID calculateID(ID start, int length, EDirection dir){
	    ID res = start ; 
	    switch(dir){
	        case VERTICAL: res = new ID(start.getX(), (char)(start.getY()+length-1)); break ; 
	        case HORIZONTAL: res = new ID(start.getX()+length-1, start.getY()); break ; 
	    }
	    return res ; 
	}
	
}
